using System;
using System.Diagnostics;


namespace RidoCode.LogManager
{

	/// <summary>
	/// Helper Class to Write Log Files based on .NET Trace Settings 
	/// </summary>
	//[System.Diagnostics.DebuggerStepThroughAttribute()]
	public  class TraceLogger : ILogger
	{
		string _category = String.Empty;
		TraceSwitch traceConfig; 		
		
		public static ILogger GetLogger(string name)
		{			
			return new TraceLogger(name);			
		}

        public static ILogger GetLogger(string name, TraceSwitch config)
        {
            return new TraceLogger(config, name);
        }

		/// <summary>
		/// Initialize the LogWriter reading settings from .Config
		/// </summary>
		public TraceLogger(string category)
		{
			_category = category;
			traceConfig	= new TraceSwitch("TraceLevel", "Controls the detail of the log");
		}

		/// <summary>
		/// Initialize the LogWriter with specied TraceSwitches
		/// </summary>
		/// <param name="config"></param>
		/// /// <param name="category"></param>
		public TraceLogger(TraceSwitch config, string category)
		{
			_category = category;
			traceConfig = config;
			Trace.AutoFlush = true;
		}
		
		public void Error(object message)
		{
			
			if ( traceConfig.TraceError) 
			{
				writeWithCurrentTime("ERROR: " + message.ToString());
			}
		}

		public void Error(object message, Exception exception)
		{		
			Error(message);
			if ( traceConfig.TraceError) 
			{
				writeException(exception);	
			}
		}

		public void Warning(object message)
		{		
		
			if ( (traceConfig.TraceWarning))
			{
				writeWithCurrentTime("WARNING: " + message.ToString());
			}			
		}

		public void Warning(object message, Exception ex)
		{		
			Warning(message);
			if ( (traceConfig.TraceWarning))
			{
				writeException(ex);
			}			
		}

		public void Info(object message)
		{
			if ((traceConfig.TraceInfo))
			{		 
				writeWithCurrentTime("INFO: " + message.ToString());
			}
		}

		public void Info(object message, Exception ex)
		{
			Info(message);
			if ((traceConfig.TraceInfo))
			{		 
				writeException(ex);
			}
		}

		private void writeWithCurrentTime(string message)
		{			
			string now = System.DateTime.Now.ToString("yy-MM-dd hh:mm:ss");						
			//Trace.WriteLine(now + " " + message, _category);									
			string traceLine = String.Format("\t {0} \t {1} \t {2}", AppDomain.GetCurrentThreadId(), now, message);
			Trace.WriteLine(traceLine, _category.PadRight(24,' '));
		}
		
		private void writeException(Exception exception)
		{
			Exception ex = exception;
			Trace.Write("EXCEPTION: ");
			Trace.IndentLevel++;
			while (ex != null)
			{
				string msg = ex.Message;
				string stack = ex.StackTrace;

				Trace.IndentLevel++;				
				Trace.WriteLineIf(msg.Length>0, msg);								
				Trace.WriteLineIf(stack!=null,stack);
				Trace.IndentLevel--;
				ex = ex.InnerException;
			}
			Trace.IndentLevel--;
		}
		
	}
}
